package cn.edu.xmut.chen.zhlg.common.framework;

import cn.edu.xmut.chen.zhlg.common.HttpResult.Result;
import cn.edu.xmut.chen.zhlg.common.framework.exception.AssertionException;
import cn.edu.xmut.chen.zhlg.common.utils.ResultType;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.stream.Collectors;

/**
 * Created by zli on 2018/4/28.
 * <p>
 * 异常处理器
 */
@ControllerAdvice
@ResponseBody
public class DefaultExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result processException(NativeWebRequest request, Exception e) {
        e.printStackTrace();
        return Result.fail(e);
    }

    /*@ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result processMaxUploadSizeExceededException(NativeWebRequest request,
        MaxUploadSizeExceededException e) {
        return APIResult.failed(ErrorCode.FILE_SIZE_OVERRUN, "文件大小超限!");
    }*/

    @ExceptionHandler(AssertionException.class)
    public Result assertException(AssertionException e) {
        e.printStackTrace();

        return Result.fail(e);
    }

    @ExceptionHandler(ValidationException.class)
    public Result processValidationException(ValidationException e) {
        e.printStackTrace();

        return Result.fail(ResultType.ILLEGAL_ARGUMENT).message(e.getMessage());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Result processConstraintViolationException(ConstraintViolationException e) {
        e.printStackTrace();

        StringBuilder sb = new StringBuilder();
        e.getConstraintViolations().forEach(cv -> {
            sb.append(cv.getMessage());
        });
        return Result.fail(ResultType.ILLEGAL_ARGUMENT).message(sb.toString());
    }

    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        e.printStackTrace();

        String message = e.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(", "));
        return Result.fail(ResultType.ILLEGAL_ARGUMENT).message(message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException e) {
        e.printStackTrace();

        BindingResult br = e.getBindingResult();
        String message = br.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(", "));
        return Result.fail(ResultType.ILLEGAL_ARGUMENT).message(message);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
        ex.printStackTrace();

        return Result.fail(ResultType.ILLEGAL_ARGUMENT).message(null);
    }
}
